#include "Sprite.h"

Sprite::Sprite(LPD3DXSPRITE SpriteHandler, LPWSTR FilePath, int Width, int Height, int Count, int SpritePerRow )
{
	D3DXIMAGE_INFO info;
	HRESULT result;

	_Image = NULL;
	_SpriteHandler = SpriteHandler;
	_Width = Width;
	_Height = Height;
	_Count = Count;
	_SpritePerRow = SpritePerRow;
	_Index = 0;

	result = D3DXGetImageInfoFromFile(FilePath, &info);
	if(result!=D3D_OK)
	{
		return;
	}

	LPDIRECT3DDEVICE9 d3ddv;
	_SpriteHandler -> GetDevice(&d3ddv);

	result = D3DXCreateTextureFromFileEx(d3ddv,
		FilePath,
		_Width,
		_Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		NULL,
		&info,
		NULL,
		&_Image);

	if(result!=D3D_OK)
	{
		return;
	}
}

Sprite::~Sprite(void){}

void Sprite::Render(LPDIRECT3DSURFACE9 Target, int x, int y, int alpha)
{
	RECT sRect;
	sRect.left = (_Index % _SpritePerRow) * _Width;
	sRect.top = (_Index / _SpritePerRow) * _Height;
	sRect.right = sRect.left + _Width;
	sRect.bottom = sRect.top + _Height;

	D3DXVECTOR3 position((float)x, (float)y, 0);
	_SpriteHandler -> Draw(_Image, 
		&sRect, 
		NULL, 
		&position, 
		D3DCOLOR_ARGB(alpha,255,255,0)
		);
}

void Sprite::Next()
{
	_Index = (_Index + 1) % _Count;
}

void Sprite::Reset()
{
	_Index = 0;
}

